<template>
  <div id="app">
    <div class="container">
      <div class="close"><span @click="$router.push('/index')" class="iconfont iconicon-test"></span></div>
      <div class="logo"><span class="iconfont iconnew"></span></div>
      <div class="inputs">
        <hmInput
          ref="username"
          v-model="user.username"
          placeholder="请输入手机号"
          :reg="/^1[3,4,5,6,7,8,9]\d{9}$/"
        ></hmInput>
        <hmInput
          ref="password"
          v-model="user.password"
          :reg="/^.{3,10}$/"
          type="password"
          placeholder="请输入密码"
        ></hmInput>
        <!-- <input placeholder="请输入手机号" class="input" /><input
          placeholder="密码"
          class="input"
          type="password"
        /> -->
      </div>
      <p class="tips">
        没有账号？
        <a href="#/register" class="">去注册</a>
      </p>
      <!-- <div icon="wap-home" class="button" round block>登录按钮</div> -->
      <van-button @click="loginFn" icon="wap-home" type="danger" round block
        >登录</van-button
      >
    </div>
  </div>
</template>

<script>
import { login } from "../api/user";
export default {
  data() {
    return {
      // 账号和密码
      user: {
        username: "18073550667",
        // username: "",
        password: "",
      },
    };
  },
  methods: {
    getData(val) {
      this.user.username = val;
    },
    // 登录
    loginFn() {
      // let arr = this.$refs.username.isOK
      // console.log(57,arr);
      if (!this.$refs.username.isOK) {
        this.$toast.fail("手机号或者密码错误");
        return;
      }
      if (!this.$refs.password.isOK) {
        this.$toast.fail("密码必须是3到10位");
        return;
      }
      login(this.user).then((res) => {
        console.log(res);
        if (res.data.message == "登录成功") {
          this.$toast.success(res.data.message);
          // 保存token和用户id
          localStorage.setItem('hm-token-71',res.data.data.token)
          localStorage.setItem('hm-id-71',res.data.data.user.id)
          // 跳转到首页
          this.$router.push('/index')
        } else {
          this.$toast.fail(res.data.message);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}
.close {
  span {
    font-size: (27 / 360) * 100vw;
  }
}
.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: (126 / 360) * 100vw;
    color: #d81e06;
  }
}
.inputs {
  input {
    // margin-bottom: 20px;
    display: block;
    margin: 0 auto 20px auto;
  }
}
.tips {
  text-align: right;
  margin-bottom: 20px;
  a {
    color: #3385ff;
  }
}
</style>